<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
  >
    <el-form ref="formAdd" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="文件链接" prop="url">
        <el-input
          v-model="form.url"
          size="mini"
          clearable
          placeholder="请输入文件链接"
        ></el-input>
      </el-form-item>
      <el-form-item label="icon" prop="icon">
        <el-input
          v-model="form.icon"
          size="mini"
          clearable
          placeholder="请输入icon"
        ></el-input>
      </el-form-item>
      <el-form-item label="文件后缀" prop="ext">
        <el-input
          v-model="form.ext"
          size="mini"
          clearable
          placeholder="请输入文件后缀"
        ></el-input>
      </el-form-item>
      <el-form-item label="文件名称" prop="name">
        <el-input
          v-model="form.name"
          size="mini"
          clearable
          placeholder="请输入文件名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="文件大小" prop="size">
        <el-input
          v-model="form.size"
          size="mini"
          clearable
          placeholder="请输入文件大小"
        ></el-input>
      </el-form-item>
      <el-form-item label="文件类型" prop="type">
        <el-input
          v-model="form.type"
          size="mini"
          clearable
          placeholder="请输入文件类型"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" active-color="#13ce66">
        </el-switch>
      </el-form-item>
      <el-form-item label="备注" prop="content">
        <el-input
          v-model="form.content"
          size="mini"
          clearable
          placeholder="请输入备注"
        ></el-input>
      </el-form-item>
      <el-form-item label="父id" prop="pid">
        <el-input
          v-model="form.pid"
          size="mini"
          clearable
          placeholder="请输入父id"
        ></el-input>
      </el-form-item>
      <el-divider />
      <mk-get-button @save="save" @close="handleClose" />
    </el-form>
  </el-dialog>
</template>

<script>
import MkGetButton from "@/components/common/mk-get-button";
import MkBackList from "@/components/common/mk-back-list";
import { dirFilesCreate, dirFilesUpdate } from "@/api/modules/sys.files.api";

export default {
  name: "get-files-info",
  components: { MkBackList, MkGetButton },
  props: {
    updateData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogTitle: "",
      type: null,
      dialogVisible: false,
      commonKey: 0,
      form: { status: false },
      isAdd: true,
      rules: {
        url: [{ required: true, message: "请输入文件链接", trigger: "blur" }],

        icon: [{ required: true, message: "请输入icon", trigger: "blur" }],

        ext: [{ required: true, message: "请输入文件后缀", trigger: "blur" }],

        name: [{ required: true, message: "请输入文件名称", trigger: "blur" }],

        size: [{ required: true, message: "请输入文件大小", trigger: "blur" }],

        type: [{ required: true, message: "请输入文件类型", trigger: "blur" }],

        status: [{ required: true, message: "请输入状态", trigger: "blur" }],
      },
    };
  },
  methods: {
    open(type, typeName, data) {
      this.resetForm();
      this.type = type;
      this.dialogTitle = typeName;
      this.dialogVisible = true;
      if (data) {
        this.form = data;
      }
    },
    save() {
      this.$refs["formAdd"].validate((valid) => {
        if (valid) {
          let _FUC = "";
          _FUC = this.type == "create" ? dirFilesCreate : dirFilesUpdate;
          _FUC(this.form)
            .then((res) => {
              this.$toast.success(
                this.type == "create" ? "添加成功！" : "修改成功！"
              );
              this.dialogVisible = false;
              this.$emit("success");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    resetForm() {
      this.form = { status: false };
    },
  },
};
</script>

<style lang="scss" scoped></style>
